package com.koron.sinoprobe.web.ledger.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProjectChange;
import com.koron.sinoprobe.web.ledger.bean.query.SpProjectChangeQuery;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectChangeVO;
import com.koron.sinoprobe.web.ledger.filter.ProjectChangeFilter;
import com.koron.sinoprobe.web.ledger.service.SpProjectChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: SpProjectChangeController
 * @projectName GDH_EAM_V2
 * @description: 项目变更
 * @date 2022/2/1116:57
 */
@Slf4j
@RestController
@RequestMapping("/sp/project/change")
@Api(tags = "专项台账-项目变更")
public class SpProjectChangeController implements WorkflowController {

    @Autowired
    private SpProjectChangeService changeService;

    @Autowired
    private ProjectChangeFilter filter;

    @GetMapping("/selectOne")
    @ApiOperation("项目变更详情查询")
    @ApiImplicitParam(name = "id", value = "项目变更id", required = true)
    public Response<SpProjectChange> selectOne(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        SpProjectChange detail = ADOConnection.runTask(user.getCurrDs(), changeService, "queryById", SpProjectChange.class, id);
        return Response.ok("查询成功", detail);
    }

    @GetMapping("/getDetailById")
    @ApiOperation("项目变更详情查询(含变更内容)")
    @ApiImplicitParam(name = "id", value = "项目变更id", required = true)
    public Response<SpProjectChangeVO> getDetailById(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        SpProjectChangeVO detail = ADOConnection.runTask(user.getCurrDs(), changeService, "getDetailById", SpProjectChangeVO.class, id);
        return Response.ok("查询成功", detail);
    }

    @GetMapping("/queryList")
    @ApiOperation("项目变更分页查询")
    public Response<PageInfo<SpProjectChange>> queryList(@CasUser @ApiIgnore EamUser user, SpProjectChangeQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<SpProjectChange> list = ADOConnection.runTask(user.getCurrDs(), changeService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改项目变更")
    public Response<SpProjectChangeVO> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody SpProjectChangeVO detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), changeService, "saveOrUpdate", SpProjectChangeVO.class, detail, null);
        return Response.ok("操作成功", detail);
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除项目变更")
    @ApiImplicitParam(name = "id", value = "项目变更id", required = true)
    public Response<String> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        ADOConnection.runTask(user.getCurrDs(), changeService, "deleteById", Boolean.class, id);
        return Response.ok("删除成功", id);
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), changeService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        SpProjectChangeVO bean = JSON.parseObject(redirectQuery.getFormVariables(), SpProjectChangeVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), changeService, "auditFinish", SpProjectChange.class, bean);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("项目变更设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("项目变更选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }
}
